﻿using System;
using System.Diagnostics;
using My.Ioc;
using My.Ioc.Core;
using My.Ioc.Exceptions;
using My.Ioc.Lifetimes;

namespace UnregisteredConcreteTypeResolution
{
    public class AutoRegistrableClass
    {
    }
    class CustomAutoRegistrationPolicy : IAutoRegistrationPolicy
    {
        #region IAutoRegistrationPolicy Member

        public bool ShouldRegister(Type concreteType)
        {
            return concreteType == typeof(AutoRegistrableClass);
        }

        public ILifetimeProvider GetLifetimeProvider()
        {
            return new ContainerLifetimeProvider();
        }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            IObjectContainer container = new ObjectContainer(false);
            AutoRegistrableClass instance;
            try
            {
                instance = container.Resolve<AutoRegistrableClass>();
            }
            catch (Exception ex)
            {
                Debug.Assert(ex is ObjectBuilderNotFoundException);
            }

            var option = new ContainerOption(false, new CustomAutoRegistrationPolicy());
            container = new ObjectContainer(option);

            instance = container.Resolve<AutoRegistrableClass>();
            Debug.Assert(instance != null);

            var instance2 = container.Resolve<AutoRegistrableClass>();
            Debug.Assert(instance2 != null);
            Debug.Assert(instance == instance2);

            Console.ReadLine();
        }
    }
}
